/**
 * Created by ZFL on 2017/12/22.
 */
    function appInit(){
    PageInit();
}
var PageInit = function () {
    $("#order-all a").click(function () {
        var index = $(this).index() + 1;
        $(this).addClass("textshow").siblings().removeClass("textshow");
        $(".tabs-" + index + "").show().siblings().hide();
    });
    //登录
    $('.but-login').bind("click", function () {
        var index = $('#order-all a[class="textshow"]').index() + 1;
        if (index == 1) {
            dynamicLogin();
        } else {
            accountLogin();
        }

    });
    //发送验证码
    $(".but").click(function () {
        sendCode()
    })
};

//账号密码登录
function accountLogin() {
    if ($("#phone").val() == "" || $("#pwd").val() == "") {
        YDUI.dialog.toast("输入不能为空",1000);
    }else if($("#phone").val().length!=11){
        YDUI.dialog.toast("手机号码输入有误",1000);
    } else {
        $.ajaxHandler({
            model: function () {
                var obj = {};
                obj.type =2; //获取微信端
                obj.userName = $("#phone").val();
                obj.password = ''+hex_md5($("#pwd").val())+'';
                return obj;
            },
            visibleTime:0,
            dataType: "json",
            api: 'yg_baijiaapp/user/login',
            success: function (result) {
                console.log(result);
                if (result.code == 10000) {
                    //保存基本信息
                    saveInfo(result.data,'登录成功');
                } else {
                    YDUI.dialog.toast(result.message,"warning");
                }
            }
        });
    }
}

//动态密码登录
function dynamicLogin() {
    if ($("#phone-id").val() == "" || $("#code-id").val() == "") {
        YDUI.dialog.toast("输入不能为空",1000);
    } else if($("#phone-id").val().length!=11){
        YDUI.dialog.toast("手机号码输入有误",1000);
    }else {
        $.ajaxHandler({
            model: function () {
                var obj = {};
                obj.type =2; //获取微信端
                obj.code = $("#code-id").val();
                obj.userName = $("#phone-id").val();
                return obj;
            },
            visibleTime: 0,
            dataType: "json",
            api: 'yg_baijiaapp/user/dynamicRegister',
            success: function (result) {
                //console.log(result);
                if (result.code == 10000) {
                    saveInfo(result.data,'登录成功');
                }
                else if (result.code == 29901) {
                    YDUI.dialog.toast("无效参数",1000);
                } else if (result.code == 20126) {
                    YDUI.dialog.toast('验证码不正确',1000);
                } else if (result.code == 20113) {
                    YDUI.dialog.toast('此用户不存在',1000);
                } else {
                    $.toptip(result.message,"warning");
                }

            }
        });
    }

}

//发送短信验证码
function sendCode() {
    if ($("#phone-id").val() == ""){
        YDUI.dialog.toast("请先输入手机号码",'error',1000);
    } else if ($("#phone-id").val().length != 11) {
        YDUI.dialog.toast("手机号码格式不正确",'error',1000);
    } else {
        $("#send-code-id").attr("disabled","disabled");
        $.ajaxHandler({
                model: function () {
                    var obj = {};
                    obj.mobile =$("#phone-id").val();
                    obj.type = 1;
                    console.log(obj);
                    return obj;
                },
                visibleTime: 0,
                dataType: "json",
                api: 'yg_baijiaapp/CheckCode/sendVerificationCode',
                success: function (result) {
                    console.log(result);
                    if (result.code == 10000) {
                        YDUI.dialog.toast("短信发送成功",'success',1000);
                        SendTime("#send-code-id");
                    } else if (result.code == 20112) {
                        YDUI.dialog.toast('短信发送失败','error',1000);
                    } else if (result.code == 20000) {
                        YDUI.dialog.toast('请求失败','error',1000);
                    } else if (result.code == 29901) {
                        YDUI.dialog.toast('无效参数','error',1000);
                    } else {
                        YDUI.dialog.toast(result.message,"warning");
                    }

                }
            }
        );
    }
}

var wait = 60;//短信时间
function SendTime(parameter) {
    var Send = $("" + parameter + "");
    if (wait == 0) {
        Send.html("| 点击获取验证码");
        wait = 60;
        $("#send-code-id").removeAttr("disabled");
    } else {
        Send.html("重新发送(" + wait + ")s");
        wait--;
        setTimeout(function () {
            SendTime(parameter);
        }, 1000);
    }
}
